package com.wy.powerbuilder.controller.ld;

import com.github.pagehelper.PageInfo;
import com.wy.powerbuilder.entity.ld.Role;
import com.wy.powerbuilder.entity.ld.User;;
import com.wy.powerbuilder.service.ld.RoleService;
import com.wy.powerbuilder.service.ld.UserService;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.persistence.Id;
import java.util.List;

@Controller
public class AdminController {
    @Autowired
    private UserService userService;
    @Autowired
    private RoleService roleService;
    @RequestMapping("admin")
    public String admin(Model model,Integer pageNo,String u_userName){
        PageInfo<User> pageInfo=userService.pageAll(pageNo,u_userName);
        model.addAttribute("pageInfo",pageInfo);
        if(u_userName==null){
            u_userName="";
        }
        model.addAttribute("name",u_userName);
        System.out.println(pageNo);
        return "admin-list";
    }
    @RequestMapping("upState")
    public String upState(Integer u_id,Integer u_state){
        System.out.println(u_id);
        System.out.println(u_state);
        if(u_state==1){
            u_state=0;
        }else {
            u_state=1;
        }
        User user=new User();
        user.setU_id(u_id);
        user.setU_state(u_state);
        int i=userService.upState(user);
        if(i>0){
            return "forward:admin";
        }
        return "500";
    }
    @RequestMapping("delUser")
    public String delUser(Integer u_id){
        int i=userService.delUser(u_id);
        if(i>0){
            return "redirect:admin";
        }
        return "500";
    }
    @RequestMapping("showAddadmin")
    public String showAddadmin(Model model){
        List<Role> roles=roleService.all();
        model.addAttribute("roles",roles);
        return "admin-add";
    }
    @ResponseBody
    @RequestMapping("adminName")
    public String adminName(String name){
        System.out.println(name);
        User user=userService.getUser(name);
        if(user==null){
            return "yes";
        }
        return "no";
    }
    @ResponseBody
    @RequestMapping("addAdmin")
    public String addAdmin(User user){
        System.out.println(user.getU_userName());
        int i=userService.addAdmin(user);
        System.out.println(i);
        if(i>0){
            return "ok";
        }else{
            return "no";
        }
    }
    @RequestMapping("showUpdateAdmin")
    public String showUpdateAdmin(int u_id,Model model){
        User user=userService.showUser(u_id);
        List<Role> roles=roleService.all();
        model.addAttribute("roles",roles);
        model.addAttribute("user",user);
        return "admin-update";
    }
    @ResponseBody
    @RequestMapping("updateAdmin")
    public String updateAdmin(User user){
        System.out.println("===="+user.getU_role());
        int i=userService.updateAdmin(user);
        System.out.println(i);
        if(i>0){
            return "ok";
        }else{
            return "no";
        }
    }
    @RequestMapping("member")
    public String member(String name,Model model){
        User user=userService.getUser(name);
        model.addAttribute("user",user);
        return "member-add";
    }
}
